let {Global} = require('Global')
let {Texas} = require('Poker')

cc.Class({
    extends: cc.Component,
    properties: {
        panels: {
            default: [],
            type: cc.Node,
        },
        countLabel: cc.Label,
        statusLabel: cc.RichText,
    },

    onLoad() {
        this.statusLabel.string = '开始下注'
    },

    bet(info) {
        let { type } = info
        let areas = this.panels
        for (let item of areas) {
            let as = item.getComponent('PublicBetArea')
            if (as.type === type) {
                as.data = info
            }
        }
    },

    startBet(countDown) {
        let localCountDown = countDown
        this.countLabel.string = '' + localCountDown
        this.schedule(() => {
            if (localCountDown === 0) {
                this.node.active = false
            } else {
                localCountDown--
                this.countLabel.string = '' + localCountDown
                /*发送当前押注*/
                let outData = new Array()
                let areas = this.panels
                for (let item of areas) {
                    let as = item.getComponent('PublicBetArea')
                    if (as.count > 0) {
                        outData.push({ betType: as.type, multiple: as.count, betMoney: 0 })
                        as.removePreBetInfo()
                    }
                }
                if (outData.length)
                    Global.gameInstance.sendPublicBetInfo(outData)
            }
        }, 1, countDown, 0)
    },

    stopCount() {
        this.unscheduleAllCallbacks()
    },

    updateResult(money) {
        if (money >= 0) {
            this.statusLabel.string = `本局你赢了<size=50><color=#fff155>${parseInt(money)}</color></size>金币`
        } else {
            this.statusLabel.string = `本局你输了<size=50><color=#ffffff>${Math.abs(parseInt(money))}</color></size>金币`
        }
    },

    updateConfig() {
        let {outConfig} = Global.room
        let keys = Object.keys(outConfig)
        console.log(keys)
        let idx = 0

        for (let item of keys) {

            let a = parseInt(item.slice(1))
            let type = a
            let panel = this.panels[idx].getComponent('PublicBetArea')
            let name = ''
            if (a > 10000) {
                type = a - 10000
                name = Texas.getCardsType(a - 10000)
            }
            panel.configData = { name: name, rate: outConfig[item], type: type }
            idx++
        }
    },

    close() {
        this.node.active = false
    },

    clear() {
        let areas = this.panels
        this.statusLabel.string = '开始下注'
        for (let item of areas) {
            let as = item.getComponent('PublicBetArea')
            as.clear()
        }
    }

})